package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallBill;
import com.sc.nft.entity.dto.MallBillAdminPageDTO;
import com.sc.nft.entity.dto.MallOrderBillAdminExcelDTO;
import com.sc.nft.entity.dto.MallOrderBillAdminPageDTO;
import com.sc.nft.enums.MallBillStatusEnum;
import com.sc.nft.mapper.MallBillMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 账单Dao
 *
 * @author liGen
 * @version 1.0
 * @date 2024/1/19 3:54 下午
 */
@Slf4j
@Repository
public class MallBillDao extends ServiceImpl<MallBillMapper, MallBill> {

    /**
     * 获取对账单列表
     *
     * @param pageNo
     * @param pageSize
     * @param startTime
     * @param endTime
     * @param statusEnum
     * @return
     */
    public Page<MallBillAdminPageDTO> getMallBillAdminPageDTO(Integer pageNo, Integer pageSize, Long daoId, Date startTime, Date endTime, String settlementNo, MallBillStatusEnum statusEnum) {
        return baseMapper.getMallBillAdminPageDTO(new Page(pageNo, pageSize), daoId, startTime, endTime, settlementNo, Objects.nonNull(statusEnum) ? statusEnum.getCode() : null);
    }

    /**
     * 获取对账单详情
     *
     * @param id
     * @return
     */
    public MallBillAdminPageDTO getDetailById(Long id) {
        return baseMapper.getDetailById(id);
    }

    /**
     * 获取对账单订单
     *
     * @param billId
     * @return
     */
    public Page<MallOrderBillAdminPageDTO> getMallOrderBillAdminPageDTOByBillId(Integer pageNo, Integer pageSize, Long billId, String productName) {
        return baseMapper.getMallOrderBillAdminPageDTOByBillId(new Page(pageNo, pageSize), billId, productName);
    }

    /**
     * 获取导出EXCEL对账单订单
     *
     * @param billId
     * @return
     */
    public Page<MallOrderBillAdminExcelDTO> getMallOrderBillAdminExcelDTOByBillId(Integer pageNo, Integer pageSize, Long billId) {
        return baseMapper.getMallOrderBillAdminExcelDTOByBillId(new Page(pageNo, pageSize), billId);
    }

    public boolean updateStatus(Long id, MallBillStatusEnum statusEnum, String remark, Date billTime) {
        UpdateWrapper<MallBill> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", statusEnum);
        if (Objects.nonNull(remark)) {
            wrapper.set("remark", remark);
        }
        if (Objects.nonNull(billTime)) {
            wrapper.set("bill_time", billTime);
        }
        return update(new MallBill(), wrapper);
    }
}
